﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
using tju.cme.BookStore2024.webapi.Models.EF;
using tju.cme.BookStore2024.webapi.Models.ViewModels.Message;

namespace tju.cme.BookStore2024.webapi.Services
{
    /// <summary>
    /// 与表T041message相关的服务
    /// </summary>
    /// <param name="context"></param>
    public class MessageServices(BookStore2024Context context)
    {
        /// <summary>
        /// 创建消息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task GreateMessageAsync(MessageCreateModel model,Guid senderId, string senderRole)
        {
            //if (model.SenderId == model.ReceiverId) throw new Exception("消息发送者和接收者不能是同一人");
            var newMeg = new T041message()
            {
                Id = Guid.NewGuid(),
                SenderId = senderId,
                ReceiverId = model.ReceiverId,
                Mcontent = model.Mcontent,
                GenerateTime = DateTime.Now,
                SenderRole = senderRole,
                ReceiverRole = model.ReceiverRole,               

            };
            
            await context.T041messages.AddAsync(newMeg);
            await context.SaveChangesAsync();
        }


        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="megId"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task SendeMessageAsync(Guid megId)
        {
            var meg = await context.T041messages.FindAsync(megId);
            if (meg == null) throw new Exception("该消息不存在");
            if(meg.IsSend) throw new Exception("该消息已发送");
            meg.IsSend = true;
            await context.SaveChangesAsync();
        }



        /// <summary>
        /// 修改消息
        /// </summary>
        /// <param name="model"></param>
        /// <param name="senderId"></param>
        /// <param name="senderRole"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task ModifyMessageAsync(MessageModifyModel model, Guid senderId, string senderRole)
        {
            var meg = await context.T041messages.FindAsync(model.Id);
            if (meg == null) throw new Exception("该消息不存在");
            if (meg.IsSend) throw new Exception("该消息已发送，不可修改");
            meg.ReceiverId = model.ReceiverId;
            meg.ReceiverRole = model.ReceiverRole;
            meg.Mcontent = model.Mcontent;
            await context.SaveChangesAsync();
        }


        /// <summary>
        /// 阅读消息
        /// </summary>
        /// <param name="megId"></param>
        /// <returns></returns>

        public async Task ReadMessageAsync(Guid megId , Guid receiverId)
        {
            var meg = await context.T041messages.FirstOrDefaultAsync( m=> m.Id == megId && m.ReceiverId == receiverId);
            
            if (meg != null && meg.IsSend == true &&meg.IsRead == false) { 
                meg.IsRead = true;
                await context.SaveChangesAsync();

            }
        }




        /// <summary>
        /// 根据用户Id,返回所有消息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IEnumerable<V041messageWith011AuthorWith022Admin>> GetAllByUserIdAsync(Guid id)
        {
            
            return await context.V041messageWith011AuthorWith022Admins.Where(m => m.ReceiverId == id || m.SenderId == id).ToListAsync();
        }



        /// <summary>
        /// 根据用户Id,返回所有已接收到的消息
        /// </summary>
        /// <param name="receiverId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<T041message>> GetReceivedByUserIdAsync(Guid id)
        {
            return await context.T041messages.Where(m => m.ReceiverId == id && m.IsSend == true).ToListAsync();
        }



        /// <summary>
        /// 根据用户Id,返回所有已发送的消息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IEnumerable<T041message>> GetSentByUserIdAsync(Guid id)
        {
            return await context.T041messages.Where(m => m.SenderId == id && m.IsSend == true).ToListAsync();
        }


        /// <summary>
        /// 根据用户Id,返回所有已读的消息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IEnumerable<T041message>> GetReadByUserIdAsync(Guid id)
        {
            return await context.T041messages.Where(m => m.ReceiverId == id && m.IsSend == true 
            && m.IsRead == true).ToListAsync();
        }


    }
}
